package com.project.purchase.Controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.purchase.Service.IDepartment;
import com.project.purchase.Service.IPerson;
import com.project.purchase.Service.IUser;
import com.project.purchase.entity.Person;
import com.project.purchase.entity.User;
import com.project.purchase.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @author VX5
 * @Title: QZP
 * @ProjectName purchase
 * @Description: TODO
 * @date ${DAT}2:04
 */
@Controller
@RequestMapping("/person")
public class PersonControlelr {

    @Autowired
    IPerson iPerson;

    @Autowired
    IUser iUser;

    @Autowired
    IDepartment iDepartment;

    @RequestMapping(value = "/hello")
    public String hello(){
        return "index";
    }

    /**
     * 显示所有
     * @param request
     * @return
     */
    @GetMapping("/get")
    public String getAll(HttpServletRequest request,
                         @RequestParam(defaultValue = "1") Integer pageNum,
                         @RequestParam(defaultValue = "5") Integer pageSize)throws Exception{
        PageHelper.startPage(pageNum,pageSize);
        List list = iPerson.GetAll();
        PageInfo pageInfo = new PageInfo(list,5);
        request.setAttribute("msg",pageInfo);
        return "test";
    }

    /**
     * 通过ID删除
     * @param request
     * @return
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping(value = "/delet")
    public String deletByID(HttpServletRequest request,
                            @RequestParam(defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "5") Integer pageSize) throws ServletException, IOException {
        PageHelper.startPage(pageNum,pageSize);
        String id = request.getParameter("id");
        iPerson.deletByID(id);
        List list = iPerson.GetAll();
        PageInfo pageInfo = new PageInfo(list,5);
        request.setAttribute("msg",pageInfo);
        return "test";
    }

    /**
     * 通过Name查找
     * @param request
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @PostMapping("/search")
    public String searchByID(HttpServletRequest request,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "5") Integer pageSize)throws Exception{
        PageHelper.startPage(pageNum,pageSize);
        String name = request.getParameter("search");
        List list = iPerson.GetByName(name);
        PageInfo pageInfo = new PageInfo(list,5);
        request.setAttribute("msg",pageInfo);
        return "test";
    }

    /**
     * 更新用户
     * @param request
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    public String updatePerson(HttpServletRequest request,
                               @RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "5") Integer pageSize)throws Exception{
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        String username = request.getParameter("username");
        String departmentname = request.getParameter("department");
        String departmentid = iDepartment.GetIDByName(departmentname);
        String tel = request.getParameter("tel");
        String id = iPerson.GetIDByName(name);
        Person person = new Person();
        person.setPersonname(name);
        person.setLoginname(username);
        person.setPassword(password);
        person.setPersonid(id);
        person.setDepartmentid(departmentid);
        person.setTel(tel);
        iPerson.updatePerson(person);
        User user = new User();
        user.setLoginname(username);
        user.setPassword(password);
        user.setParam("user");
        iUser.updateUser(user);
        PageHelper.startPage(pageNum,pageSize);
        List list = iPerson.GetAll();
        PageInfo pageInfo = new PageInfo(list,5);
        request.setAttribute("msg",pageInfo);
        return "test";
    }

    /**
     * 增加用户
     * @param request
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public String addPerson(HttpServletRequest request,
                            @RequestParam(defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "5") Integer pageSize)throws Exception{
        String id = CommonUtils.getUUID();
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        String username = request.getParameter("username");
        String departmentname = request.getParameter("department");
        String departmentid = iDepartment.GetIDByName(departmentname);
        String tel = request.getParameter("tel");
        Person person = new Person();
        person.setPersonid(id);
        person.setPersonname(name);
        person.setLoginname(username);
        person.setPassword(password);
        person.setDepartmentid(departmentid);
        person.setTel(tel);
        iPerson.addPerson(person);
        User user = new User();
        user.setLoginname(username);
        user.setPassword(password);
        user.setParam("user");
        user.setLoginid(id);
        iUser.addUser(user);
        PageHelper.startPage(pageNum,pageSize);
        List list = iPerson.GetAll();
        PageInfo pageInfo = new PageInfo(list,5);
        request.setAttribute("msg",pageInfo);
        return "test";
    }



}
